﻿using HrEFCore;
using HrService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace HrUI.Controllers
{
	public class CFSKController : Controller
	{
		private readonly MyDbContext myDbContext;
		private readonly CFSKService service;
		private readonly CFFKService cffkService;

		public CFSKController(MyDbContext myDbContext,CFSKService service,CFFKService cffkService)
        {
            this.myDbContext = myDbContext;
			this.service = service;
			this.cffkService = cffkService;
		}
		//查看界面
        public async Task<ActionResult> Index()
		{
			List<CFSK> list = await service.CFSKSelectAsync();
			ViewData.Model = list;
			return View();
		}
		//显示添加
		public async Task<ActionResult> Insert()
		{

			List<CFFK> list = await cffkService.CFFKSelectAsync();
			SelectList selectListItems = new SelectList(list, "FirstKindId", "FirstKindName", "FirstKindName");
			ViewBag.s = selectListItems;
			return View();
		}
		//添加数据
		public async Task<ActionResult> CFSKAdd(CFSK cfsk)
		{
			CFFK cffk = await service.CFSKById2(cfsk.FirstKindId);
			cfsk.FirstKindName = cffk.FirstKindName;

			int row = await service.CFSKInsertAsync(cfsk);
			if (row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				List<CFFK> list = await cffkService.CFFKSelectAsync();
				SelectList selectListItems = new SelectList(list, "FfkId", "FirstKindName");
				ViewBag.s = selectListItems;
				return View();
			}
		}
		//删除
		public async Task<ActionResult> CFSKDel(short id)
		{
			int row = await service.CFSKDelAsync(id);
			if (row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				return RedirectToAction("Index");
			}
		}

		public async Task<ActionResult> Update(short id)
		{
			CFSK cfsk = await service.CFSKById(id);
			ViewData.Model = cfsk;
			List<CFFK> list = await cffkService.CFFKSelectAsync();
			SelectList selectListItems = new SelectList(list, "FirstKindId", "FirstKindName");
			ViewBag.s = selectListItems;
			return View();
		}
		public async Task<ActionResult> CFSKUpdate(CFSK cfsk)
		{
			CFFK cffk = await service.CFSKById2(cfsk.FirstKindId);
			cfsk.FirstKindId = cffk.FirstKindId;
			cfsk.FirstKindName = cffk.FirstKindName;
			int row = await service.CFSKUpdateAsync(cfsk);
			if (row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				List<CFFK> list = await cffkService.CFFKSelectAsync();
				SelectList selectListItems = new SelectList(list, "FirstKindId", "FirstKindName");
				ViewBag.s = selectListItems;
				return View();
			}
		}
	}
}
